package leetcode38

import (
	"strconv"
)

func countAndSay(n int) string {
	intput := "1"
	for i := 0; i < n-1; i++ {
		intput = Say(intput)
	}
	return intput
}

func Say(input string) (out string) {
	intStr := input
	var (
		curByte byte
		num     int
		res     string
	)

	if len(intStr) == 1 {

		return res + "1" + intStr
	}

	curByte = intStr[0]
	for i := range intStr {
		if intStr[i] == curByte {
			num++
		} else {
			res += strconv.Itoa(num) + string(intStr[i-1])
			curByte = intStr[i]
			num = 1
		}
	}
	return res + strconv.Itoa(num) + string(curByte)
}
